/*
 * @lc app=leetcode.cn id=61 lang=javascript
 *
 * [61] 旋转链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} k
 * @return {ListNode}
 */
var rotateRight = function(head, k) {
    if(head==null)return null
    let res=[]
    while(head){
        res.push(head.val)
        head=head.next
    }
    
    let len=res.length
    while(k>=len){
        k=k-len
    }
     res=res.concat(res)
     res=res.slice(len-k,2*len-k)
    let tail=new ListNode(null)
    head=tail
    while(res.length){
        let temp=res.shift()
        tail.next=new ListNode(temp)
        tail=tail.next
    }
    return head.next
};
// @lc code=end

